package configuration

import (
	"encoding/json"
	"kop/modules/configuration/client"
	"kop/util/log"
	"sort"
)

func init() {

	var err error
	var b = client.Load("BarEvent")
	if err = json.Unmarshal(b, &BarEventConfigs); err != nil {
		panic(err)
	}

	log.Info("Load BarEvent.json success.")

	//********************************************************************//

	//********************************************************************//

	var m = make(map[int32]BarCostConf)

	b = client.Load("BarCost")
	if err = json.Unmarshal(b, &m); err != nil {
		panic(err)
	}

	log.Info("Load BarCost.json success.")

	for _, v := range m {
		BarCostConfigs = append(BarCostConfigs, v)
	}
	sort.Sort(BarCostConfigs)
}

const (
	Captain       = 1
	MaidNotFriend = 2
	MaidFriend    = 3
)

type (
	BarEventType int8

	// 结交侍女概率
	BarEventConf struct {
		ID        int32        // 事件ID
		Weight    int32        // 权重
		Type      BarEventType // 事件类型
		CaptainID int32        // 船长ID
		MaidID    int32        // 侍女ID
	}

	// 请酒消耗
	BarCostConf struct {
		ID       int32 // 索引ID
		Count    int32 // 许愿次数
		CostCoin int64 // 许愿消耗银币数量
		CostGold int32 // 许愿消耗金币数量
	}
)

func (c BarEventConf) GetID() int32 {
	return c.ID
}

func (c BarEventConf) GetWeight() int32 {
	return c.Weight
}

var (
	BarEventConfigs = map[int32]BarEventConf{}
	BarCostConfigs  BarCosts
)

type BarCosts []BarCostConf

func (c BarCosts) Len() int           { return len(c) }
func (c BarCosts) Swap(i, j int)      { c[i], c[j] = c[j], c[i] }
func (c BarCosts) Less(i, j int) bool { return c[i].Count < c[j].Count }

func GetByWishNum(n int32) BarCostConf {

	for _, v := range BarCostConfigs {
		if n < v.Count {
			return v
		}
	}

	return BarCostConfigs[0]
}
